/**
 * @returns 路由跳转参数装换
 */
export const transitionParams = (params = {}, url = "", row = {}) => {
  const arr = []
  Object.keys(params).forEach(key => {
    arr.push(`${key}=${params[key] ?? row[key] ?? ""}`)
  })
  return url + "?" + arr.join("&")
}

/**
 * @param {Array} originArr 原数组
 * @param {String} label 显示的字段
 * @param {String} value 提交的内容
 * @returns 转换为下拉框需要数据
 */
export const changeArrField = (originArr, label, value) => {
  if (!originArr || !originArr.length) {
    return []
  }

  return originArr.map(item => {
    return {
      text: item[label],
      value: item[value] || item[label]
    }
  })
}

/**
 * @returns 对pc端添加指定样式
 */
export const getDeviceModel = () => {
  const deviceModel = uni.getSystemInfoSync().deviceModel
  if (deviceModel === "PC") {
    const body = document.querySelector("html")
    body.className += " custom-pc"
  }
}

/**
 * @returns 验证是否是pc端
 */
export const verifyPc = () => {
  const deviceModel = uni.getSystemInfoSync().deviceModel

  return deviceModel === "PC"
}

/**
 * @param {String} field 替换前内容
 * @param {String} value 需要替换的内容
 * 内容替换
*/
export const renderStr = (field, data) => {
  const result = field.replace(/\{\{(\w+)\}\}/g, (findStr, $1) => {
    return data[$1];
  });
  return result;
};
